#include "CL_News_pool.h"

const std::string CL_News_pool::CL_RSS_Quelle::accepted_mime_types=
     "application/xhtml+xml; q=0.5, text/xml; q=0.6, application/rss+xml; q=0.8, application/atom+xml; q=0.9, application/xml";


CL_News_pool::CL_RSS_Quelle::QError::QError():ErrorDescription("") {};
CL_News_pool::CL_RSS_Quelle::QError::QError(const std::string& c):ErrorDescription(c) {};



CL_News_pool::CL_RSS_Quelle::CL_RSS_Quelle(const std::string& u):last_connection(0),last_feed_hash(0), last_news_hash(),
     url(u), is_erroneous(false), last_error(""), last_modified("")
{

};
CL_News_pool::CL_RSS_Quelle::CL_RSS_Quelle():last_connection(0),last_feed_hash(0), last_news_hash(),
     url(""), is_erroneous(false), last_error(""), last_modified("")
{

};

CL_News_pool::CL_RSS_Quelle::operator bool() const
{
     return url==""?false:true;
};

void CL_News_pool::CL_RSS_Quelle::set_url(const std::string& s)
{
     if(s!=url) {
          url=s;
          last_news_hash.clear();
          is_erroneous = false;
          last_error.set_error("");
          last_feed_hash=0;
     };
};
CL_News_pool::CL_RSS_Quelle& CL_News_pool::CL_RSS_Quelle::operator=(const CL_News_pool::CL_RSS_Quelle& q)
{
     if(this!=&q) {
          last_connection=q.last_connection;
          last_feed_hash=q.last_feed_hash;
          last_news_hash=q.last_news_hash;
          url=q.url;
          is_erroneous=q.is_erroneous;
          last_error=q.last_error;
     }
     return *this;
}

CL_News_pool::CL_RSS_Quelle::~CL_RSS_Quelle()
{
}




int CL_News_pool::CL_RSS_Quelle::create_news_from_xml(std::string& xml_utf8, newsmap& map)
{
     pugi::xml_document rss_doc;
     std::istringstream xml_utf8_stream(xml_utf8);
     pugi::xml_parse_result result = rss_doc.load(xml_utf8_stream,pugi::parse_default,pugi::encoding_utf8);
     pugi::xml_node version=rss_doc.child("rss");
     if (version) {
//can be RSS
          pugi::xml_attribute version_att=version.attribute("version");
          if(version_att) {
//RSS 2.0?
               if(strcmp(version_att.value(),"2.0")) {
                    last_error.set_error("XML Paring: couldn't find rss version");
                    throw last_error;
               }
//okay: RSS 2.0 steam found
               return parse_rss_20(map, rss_doc, version);
          } else {
               last_error.set_error("XML Paring: couldn't find rss version");
               throw last_error;
          }
     } else {
          pugi::xml_node feed=rss_doc.child("feed");
          if(feed) {
               return parse_atom(map, rss_doc, feed);
          } else {
               last_error.set_error("XML Paring: couldn't find rss version");
               throw last_error;
          }
     }
}

newsmap& operator+(newsmap& m, CL_News_pool::CL_RSS_Quelle& q)
{
     if (q.is_erroneous||(time(NULL)-q.last_connection)<120 )return m;
     q.last_connection=time(NULL);
     std::string s="";
     CL_News_pool::CL_RSS_Quelle::iNetReturn iNR=q.get_response_utf8(s);
     if(iNR.status!=Poco::Net::HTTPResponse::HTTP_OK||
               iNR.internal_error!=CL_News_pool::CL_RSS_Quelle::INT_ERR_ALL_OKAY
               ||iNR.hashvalue==q.last_feed_hash) return m;
     q.last_feed_hash=iNR.hashvalue;
     q.create_news_from_xml(s, m);
     return m;
}

newsmap& operator+=(newsmap& m, CL_News_pool::CL_RSS_Quelle& q)
{
     return m=m+q;
}






